<!--跳转到主题的链接-->
<script setup>

import {setupMapState} from "@/assets/js/vuexUtils";
import {routeNames} from "@/router";
import querystring from "querystring";
import NgaRouterLink from "@/components/common/link/NgaRouterLink.vue";
import SearchLink from "@/components/common/link/SearchLink.vue";

const props = defineProps({
  tid: Number,
  pid: Number,
  page: Number,
  authorId: Number,
  title: String,
  bracket: Boolean,

  //版面主题还是合集主题
  type: {type: String},
  // 版面或合集的id
  targetId: {type: Number},
})

const {currentUid} = setupMapState("Account", ['currentUid']);

const titlePattern = /(\[.+?])/g

const getRoute = ({tid, pid, page, authorId}) => {
  const topicParam = {
    name: routeNames.read.topic,
    params: {id: tid},
    query: {page, authorId, account: currentUid.value},
  }
  // 跳转到某一页的指定楼层
  if (tid && pid && page) {
    topicParam.hash = "#P" + pid
    return topicParam
  }
  // 打开某一个单独回复
  if (pid) {
    return {name: routeNames.read.reply, params: {id: pid}, query: {account: currentUid.value}}
  }
  // 常规页
  return topicParam
}

// 搜索标签
const getTags = (title) => {
  const res = []
  //     解析标题中的tag
  let group;
  while (group = titlePattern.exec(title)) {
    res.push(group[1])
  }
  return res;
}
// 官方地址
const getUrl = ({tid, pid, page, authorId}) => {
  if (pid !== undefined && pid !== 0) {
    //     pid模式
    return `https://bbs.nga.cn/read.php?pid=${pid}&to`
  }
//     tid模式
  const param = {tid}
  if (page) param.page = page
  if (authorId) param.authorid = authorId
  return 'https://bbs.nga.cn/read.php?' + querystring.stringify(param)
}

</script>

<template>
  <span>
    <el-tooltip effect="light" placement="top-start">
      <nga-router-link v-if="tid||pid" :bracket="bracket" :route="getRoute({tid, pid,page,authorId})">
        <slot></slot>
      </nga-router-link>
      <slot v-else></slot>

      <template #content>
        <!-- 搜索标签提示-->
        <template v-for="item in getTags(title)" v-if="title&&type && targetId">
          <search-link :keyword="item" :target-id="targetId" :type="type">
            {{ item }}
          </search-link>
        </template>
        <div>
          <el-link :href="getUrl({tid, pid,page,authorId})" target="_blank" type="primary">官方地址</el-link>
        </div>
      </template>
    </el-tooltip>
  </span>
</template>

<style scoped>

</style>